/**
 * Copyright © 2017, Forp Co., LTD
 *
 * All Rights Reserved.
 */
package com.mvc.core.utils;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.reflect.FieldUtils;

import com.alibaba.fastjson.JSONArray;

/**
 * 
 * 经常使用的逻辑代码
 *
 * @author lt
 * @version 2017年10月30日下午5:45:31
 */
public class LogicCode {

	/**
	 * sql in 组装方法
	 * 
	 * @param params 逗号分割的参数
	 * @param sql 拼接的sql
	 * @param fileName 匹配的字段
	 * @param josn JOSN
	 * @return
	 */
	public static String sqlFmt2In(String params, String sql, String fileName, JSONArray josn) {
		String[] explor = params.split(",");
		StringBuffer sb = new StringBuffer(sql);
		if (null != explor && explor.length > 0) {
			sb.append(new StringBuffer(" and " + fileName + " in ("));
			for (int i = 0; i < explor.length; i++) {
				if (i == explor.length - 1) {
					sb.append(" ?");
				} else {
					sb.append(" ?,");
				}
				josn.add(explor[i]);
			}
			sb.append(" )");
		}
		return sb.toString();
	}

	/**
	 * sql in 组装方法
	 * 
	 * @param params 逗号分割的参数
	 * @param sql 拼接的sql
	 * @param fileName 匹配的字段
	 * @param josn JOSN
	 * @return
	 * @return
	 */
	public static String sqlFmt2In(String[] params, StringBuffer sb, String fileName, JSONArray josn) {
		if (null != params && params.length > 0) {
			sb.append(new StringBuffer(" and " + fileName + " in ("));
			for (int i = 0; i < params.length; i++) {
				if (i == params.length - 1) {
					sb.append(" ?");
				} else {
					sb.append(" ?,");
				}
				josn.add(params[i]);
			}
			sb.append(" )");
		}
		return sb.toString();
	}

	/**
	 * 判断对象为空
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isEmpty(Object obj) {
		if (obj == null)
			return true;

		if (obj instanceof CharSequence)
			return ((CharSequence) obj).length() == 0;

		if (obj instanceof Collection)
			return ((Collection) obj).isEmpty();

		if (obj instanceof Map)
			return ((Map) obj).isEmpty();

		if (obj instanceof String)
			return "".equals(obj);

		if (obj instanceof Object[]) {
			Object[] object = (Object[]) obj;
			if (object.length == 0) {
				return true;
			}
			boolean empty = true;
			for (int i = 0; i < object.length; i++) {
				if (!isEmpty(object[i])) {
					empty = false;
					break;
				}
			}
			return empty;
		}
		return false;
	}

	/**
	 * 判断对象不为空
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isNotEmpty(Object obj) {
		return !isEmpty(obj);
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param smdate 较小的时间
	 * @param bdate 较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 字符串转日期
	 * 
	 * @param strDate 字符串日期
	 * @param pattern 日期格式
	 * @return
	 * @throws ParseException
	 */
	public static Date string2Date(String strDate, String pattern) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date date = sdf.parse(strDate);
		return date;
	}

	/**
	 * 日期转字符串
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */

	public static String date2String(Date date, String pattern) throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		return formatter.format(date);
	}

	/**
	 * 快照日期转换
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static void date2Timestamp(Object model) throws Exception {
		if (isEmpty(model)) {
			return;
		}
		// 获取实体类的所有属性，返回Field数组
		Field[] field = model.getClass().getDeclaredFields();
		// 遍历所有属性
		for (int j = 0; j < field.length; j++) {
			// 获取属性的名字
			String name = field[j].getName();
			// 将属性的首字符大写，方便构造get，set方法
			if (!"serialVersionUID".equals(name)) {
				// 调用getter方法获取属性值
				Object value = FieldUtils.readDeclaredField(model, name, true);
				if (value != null) {
					if (value instanceof Date) {
						FieldUtils.writeDeclaredField(model, name, new Timestamp(((Date) value).getTime()), true);
					}
				}
			}
		}
	}

}
